;
(function() {
	var skill = {
		save: function() {
			db.update(config.dbname.ticketFactory, this._id, this);
		},
		destroy: function() {
			db.remove(config.dbname.ticketFactory, this._id);
		},
		setInfo: function(info) {
			var self = this;
			//名称
			var name = String(info.name || "【优惠券】").trim();
			name = name.substr(0, 20); //限制不能超过20个字符
			if (!helper.folder_name(name)) {
				throwCusError("优惠券名不规范")
			}
			this.info.name = name;

			//可领取几张
			var ticket_list_limit = +info.ticket_list_limit || Number.MAX_VALUE;
			this.info.ticket_list_limit = ticket_list_limit;

			//可领取的时间区间
			var get_ticket_start_time = ~~info.get_ticket_start_time;
			var get_ticket_end_time = ~~info.get_ticket_end_time || Number.MAX_VALUE;
			this.info.get_ticket_start_time = get_ticket_start_time;
			this.info.get_ticket_end_time = get_ticket_end_time;

			//可用次数
			var times = ~~info.times || 1;
			this.info.times = times;

			//过期时间
			var diedline = +info.diedline || Number.MAX_VALUE;
			this.info.diedline = diedline;

			//类型
			var type = +info.type;
			if (!type && TicketFactoryCon.TYPE.hasOwnValue(type)) {
				throwCusError("找不到指定优惠券类型")
			}
			this.info.type = type;
			//类型对应的相关配置
			//打折：折扣比例，是否有相关梯度
			//满减：满多少，减多少，是否有相关梯度
			//抵用：可抵用的现金额度，是否有相关梯度
			var config = info.config || {};
			this.info.config = config;

			//适用的范围
			var bus = BusCon.get(this.bus_id);
			//指定商品适用
			var goods_limit = Array.isArray(info.goods_limit) ? info.goods_limit : null;
			this.info.goods_limit = goods_limit && info.goods_limit.filter(function(goods_id) {
				return bus.hasGoodsById(goods_id);
			});
			//指定标签适用
			var classify_limit = Array.isArray(info.classify_limit) ? info.classify_limit : null;
			this.info.classify_limit = classify_limit && info.classify_limit.filter(function(tag_id) {
				return bus.hasClassifyById(tag_id);
			});

			//描述
			var intro = String(info.intro || "").trim();
			this.info.intro = intro || this.getIntro();

			//是否可以被用户直接领取，只能商家发放或者抽奖领取等其它渠道
			this.info.forbid_user_get = helper.boolean_parse(info.forbid_user_get);

			this.save();
		},
		getIntro: function() {
			return TicketFactoryCon.getIntro(this);
		},
		hasTicket_ByUserId: function(user_id) {
			return this.ticket_list.some(function(ticket_id) {
				return TicketFactoryCon.ticketId_has_user(ticket_id, user_id)
			});
		},
		//生成针对某个用户的优惠券
		toTicket: function(user_id) {
			if (this.hasTicket_ByUserId(user_id)) {
				throwCusError("指定用户已经有此优惠券，不可重复领取");
			} else {
				var ticket = new TicketCon({
					user_id: user_id,
					bus_id: this.bus_id,
					ticket_factory_id: this._id
				});
				this.ticket_list.push(this._id);
				this.save();
			}
			return ticket;
		},
	};
	module.exports = skill;

	var db = require("../db/index");
	var TicketCon = require("../ticket/_con");
	var TicketFactoryCon = require("../ticket_factory/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
}());